package tech.dx.springdb.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.dx.springdb.entity.LearningStatus;
import tech.dx.springdb.service.LearningStatusService;

import java.util.Optional;

/**
 * @ClassName LearningStatusController
 * @Description TODO
 * @Author daoxuan
 * @Date 2019/4/2 18:14
 * @Version 1.0
 */
@RestController
@RequestMapping("learningstatus")
@Api(tags = {"学习状态"})
public class LearningStatusController {
    @Autowired
    private LearningStatusService learningStatusService;

    @GetMapping("/{id}")
    public LearningStatus get(@PathVariable long id) {
        Optional<LearningStatus> byId = learningStatusService.getById(id);
        if (byId.isPresent()) {
            return byId.get();
        } else {
            return null;
        }
    }

    @PostMapping
    public LearningStatus add(@RequestBody LearningStatus learningStatus) {
        return learningStatusService.add(learningStatus);
    }

    @PutMapping("/{id}")
    public LearningStatus update(@RequestBody LearningStatus learningStatus, @PathVariable long id) {
        if (learningStatusService.isExitById(id)) {
            learningStatus.setId(id);
            return learningStatusService.update(learningStatus);
        } else {
            return null;
        }
    }

    @DeleteMapping("/{id}")
    public void del(@PathVariable long id) {
            learningStatusService.del(id);
    }
}
